/* Vlcmm plugin */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gtkmm/filechooserdialog.h>
#include <gtkmm/filefilter.h>
#include <gtkmm/label.h>
#include <gtkmm/scrolledwindow.h>
#include <gtkmm/spinbutton.h>
#include <gtkmm/stock.h>
#include <gtkmm/textbuffer.h>

#include "messages.hh"

#include "util/string.hh"

static Glib::RefPtr<Glib::IOChannel> file;

MessagesDialog::MessagesDialog (intf_thread_t *_p_i, Gtk::Window *parent) : m_text_view (), m_toplevel (parent), p_intf (_p_i), m_notebook ()
{

  if (m_toplevel)
    set_transient_for (*m_toplevel);

  set_title (_("Messages"));
  set_has_separator (false);
  set_border_width (4);

  // - - - - - -

  m_notebook.append_page (*Gtk::manage (MessagesDialog::create_msg_tab ()), _("Messages"), true);
  m_notebook.append_page (*Gtk::manage (MessagesDialog::create_module_tree_tab ()), _("Modules tree"), true);

  m_notebook.signal_switch_page ().connect (sigc::mem_fun (this, &MessagesDialog::switch_page));

  get_vbox ()->pack_start (m_notebook);

  // - - - - - -

  m_save_button = add_button (Gtk::Stock::SAVE_AS, 0);
  m_save_button->signal_clicked ().connect (sigc::mem_fun (this, &MessagesDialog::save));

  m_clear_button = add_button (Gtk::Stock::CLEAR, 0);
  m_clear_button->signal_clicked ().connect (sigc::mem_fun (this, &MessagesDialog::clear));

  m_refresh_button = add_button (Gtk::Stock::REFRESH, 0);
  m_refresh_button->signal_clicked ().connect (sigc::mem_fun (this, &MessagesDialog::updateTree));

  add_button (Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE)->signal_clicked ().connect (sigc::mem_fun (this, &MessagesDialog::close));

  // - - - - - -

  show_all ();

}

MessagesDialog::~MessagesDialog ()
{}

void
MessagesDialog::switch_page (GtkNotebookPage* page, guint page_num)
{

  if (page_num == 0)
   {
     m_refresh_button->hide ();
     m_save_button->show ();
     m_clear_button->show ();
   }
  else
   {
    m_refresh_button->show ();
    m_save_button->hide ();
    m_clear_button->hide ();
   }

}

void
MessagesDialog::updateLog ()
{
    //delete this;
}

void
MessagesDialog::close ()
{

  delete this;

}

Gtk::VBox*
MessagesDialog::create_msg_tab ()
{

  Gtk::VBox *vbox = new Gtk::VBox (false, 4);
  vbox->set_border_width (4);

  Glib::RefPtr<Gtk::TextBuffer> text_buffer = Gtk::TextBuffer::create ();

  Gtk::ScrolledWindow *scrolled_window = new Gtk::ScrolledWindow ();
  scrolled_window->set_shadow_type (Gtk::SHADOW_ETCHED_OUT);
  scrolled_window->set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);

  m_text_view.set_editable (false);

  text_buffer->set_text ("Test");

  m_text_view.set_buffer (text_buffer);

  scrolled_window->add (m_text_view);

  vbox->pack_start (*Gtk::manage (scrolled_window));

  Gtk::HBox *hbox = new Gtk::HBox (false, 4);

  hbox->pack_start (*new Gtk::Label (_("Verbosity Level")), false, false, 4);

  Gtk::SpinButton *spin_button = new Gtk::SpinButton ();
  spin_button->set_numeric (true);
  spin_button->set_range (0, 2);
  spin_button->set_value (config_GetInt (p_intf, "verbose"));
  spin_button->set_wrap (true);
  hbox->pack_start (*Gtk::manage (spin_button), false, false, 4);

  vbox->pack_start (*Gtk::manage (hbox), false, false);
  
  return vbox;
  
}

Gtk::VBox*
MessagesDialog::create_module_tree_tab ()
{

  Gtk::VBox *vbox = new Gtk::VBox (false, 4);
  vbox->set_border_width (4);

  return vbox;
  
}

void
MessagesDialog::updateTree ()
{

  delete this;

}

void
MessagesDialog::clear ()
{

  Glib::RefPtr<Gtk::TextBuffer> text_buffer = m_text_view.get_buffer ();

  text_buffer->erase (text_buffer->begin (), text_buffer->end ());

}

void
MessagesDialog::save ()
{

  Glib::RefPtr<Gtk::TextBuffer> text_buffer = m_text_view.get_buffer ();

  Gtk::FileChooserDialog *fc = new Gtk::FileChooserDialog (*m_toplevel, _("Choose a filename to save the logs under..."), Gtk::FILE_CHOOSER_ACTION_SAVE);
  
  fc->add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
  fc->add_button (Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
  fc->set_current_folder_uri (Glib::filename_to_uri (p_intf->p_libvlc->psz_homedir));
  
  Gtk::FileFilter *file_filter = new Gtk::FileFilter ();
  file_filter->set_name (_("Texts / Logs (*.log *.txt)"));
  file_filter->add_pattern ("*.log");
  file_filter->add_pattern ("*.txt");
  fc->add_filter (*file_filter);

  file_filter = new Gtk::FileFilter ();
  file_filter->set_name (_("All (*.*)"));
  file_filter->add_pattern ("*");
  fc->add_filter (*file_filter);

  switch (fc->run ())
   {   
    case Gtk::RESPONSE_ACCEPT:
    
      file = Glib::IOChannel::create_from_file (fc->get_filename (), "w");
    
      file->write (text_buffer->get_text ());
      file->flush ();
 
    break;
    
    default:
     break;
   }
  
  delete fc;  

}

